# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_pyname=Pygments
_realname=pygments
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=2.7.4
pkgrel=1
pkgdesc="Python syntax highlighter (mingw-w64)"
arch=('any')
license=('BSD')
url="http://pygments.org/"
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://pypi.org/packages/source/P/Pygments/${_pyname}-${pkgver}.tar.gz")
install=${_realname}3-${CARCH}.install
noextract=("${_pyname}-${pkgver}.tar.gz")
sha256sums=('df49d09b498e83c1a73128295860250b0b7edd4c723a32e9bc0d295c7c2ec337')

prepare() {
  # workaround for symlink issue
  tar -xf ${_pyname}-${pkgver}.tar.gz || true
  cp ${_pyname}-${pkgver}/scripts/debug_lexer.py ${_pyname}-${pkgver}/scripts/find_error.py

  rm -rf python-build-${CARCH} | true
  cp -r ${_pyname}-${pkgver} python-build-${CARCH}
}

build () {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  msg "Python test for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py check
}

package() {
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" -O1 --skip-build

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
